package com.jhx.demos.web.controller;

//
//import com.jhx.demos.web.Utils.JwtUtils;
import com.jhx.demos.web.anno.Log;
import com.jhx.demos.web.pojo.BufferMessage;
import com.jhx.demos.web.pojo.PageBean;
import com.jhx.demos.web.pojo.Result;
import com.jhx.demos.web.pojo.Message;
import com.jhx.demos.web.service.userService;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.List;

@Slf4j
@RequestMapping("/user")
@RestController
public class userController {

    //注入浏览器请求获取jwt令牌
    @Autowired
    private HttpServletRequest Request;

    @Autowired
    private userService userService;

//    @GetMapping
//    public Result list() {
//        log.info("查询全部的部门数据");
//
//        //调用service查询数据
//        List<Message> deptList = userService.list();
//
//        //响应一个成功的结果
//        return Result.success(deptList);//返回回去的数据
//    }

    @GetMapping
    public Result page(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            Integer publisherID, String content,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDate end) {
        log.info("分页查询,参数:{},{},{},{},{},{}", page, pageSize, publisherID, content, begin, end);

        //调用service方法进行分页查询
        PageBean pageBean = userService.page(page, pageSize, publisherID, begin, end);//调用service层传递参数
        return Result.success(pageBean);
    }

    //新增留言到缓冲区!!!
    @Log
    @PostMapping
    public Result save(@RequestBody BufferMessage bmes) {
        log.info("用户进行新增留言 buffermessage:{}", bmes);
        String jwt = Request.getHeader("token");
//        Claims claims = JwtUtils.parseJWT(jwt);
        userService.save(bmes);
        return Result.success();
    }

    //删除留言
    @Log
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("批量删除操作, ids:{}", ids);
        String jwt = Request.getHeader("token");
//        Claims claims = JwtUtils.parseJWT(jwt);
        userService.delete(ids);
        return Result.success();
    }


    //根据id查询留言信息
    @GetMapping("/{id}")
    public Result find(@PathVariable Integer id) {//这是路径参数要加@PathVariable
        log.info("根据id查询留言信息 ,id: {}", id);
        String jwt = Request.getHeader("token");
//        Claims claims = JwtUtils.parseJWT(jwt);
        Message mes = userService.find(id);//将id传进来根据id查询员工信息
        return Result.success(mes);
    }

    //修改留言信息
    @Log
    @PutMapping
    public Result update(@RequestBody Message mes) {//用对象接收前端发来的json文件格式并用@RequestBody接收
        log.info("修改信息 {}", mes);
        String jwt = Request.getHeader("token");
//        Claims claims = JwtUtils.parseJWT(jwt);
        userService.update(mes);
        return Result.success();
    }

}
